//
//  MovieTableViewCell.m
//  快乐一点通
//
//  Created by cjp on 16/8/12.
//  Copyright © 2016年 cjp. All rights reserved.
//

#import "MovieTableViewCell.h"
#import "MovieModel.h"
#import <Masonry.h>
#import <AVFoundation/AVFoundation.h>
#import <UIImageView+WebCache.h>
#import "MovieDetailViewController.h"

@interface MovieTableViewCell ()
/** 中间图片 */
//@property (weak, nonatomic) IBOutlet UIImageView *imgView;
/** 播放次数 */
@property (weak, nonatomic) IBOutlet UIButton *play_countButton;
/** 点赞次数 */
@property (weak, nonatomic) IBOutlet UIButton *vote_countButton;
/** 评论次数 */
@property (weak, nonatomic) IBOutlet UIButton *comment_countButton;
/** 标题 */
@property (weak, nonatomic) IBOutlet UILabel *movieTextLabel;


@end

@implementation MovieTableViewCell

- (void)awakeFromNib{
    
    self.playVideo.tag = 101;
    self.playVideo.userInteractionEnabled = YES;
    // 代码添加playerBtn到imageView上
    self.playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.playBtn setImage:[UIImage imageNamed:@"播放-1"]forState:UIControlStateNormal];
    [self.playBtn setImage:[UIImage imageNamed:@"播放"]forState:UIControlStateHighlighted];
    
    [self.playBtn addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
    [self.playVideo addSubview:self.playBtn];
    [self.playBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.playVideo);
    }];
}

- (void)setMovie:(MovieModel *)movie{
    
    _movie = movie;
    
    self.movieTextLabel.text = movie.title;
    NSURL *url = [NSURL URLWithString:movie.image];
    NSData *data = [[NSData alloc]initWithContentsOfURL:url];
    UIImage *img = [UIImage imageWithData:data];
    [self.playVideo sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",movie.first_url]] placeholderImage:img];
    
    [self.play_countButton setTitle:[NSString stringWithFormat:@"%zd",movie.play_count] forState:UIControlStateNormal];
    [self.vote_countButton setTitle:[NSString stringWithFormat:@"%zd",movie.vote_count] forState:UIControlStateNormal];
    [self.comment_countButton setTitle:[NSString stringWithFormat:@"%zd",movie.comment_count] forState:UIControlStateNormal];
    
}

- (void)play:(UIButton *)sender {
    
    if (self.playBlock) {
        self.playBlock(sender);
    }
    
}

- (void)setFrame:(CGRect)frame{
    
    frame.origin.x = MovieCellMargin;
    frame.size.width -= 2 * frame.origin.x;
    frame.size.height -= MovieCellMargin - 2;
    frame.origin.y += MovieCellMargin;
    
    [super setFrame:frame];
}








@end
